package com.zkx.system.generator.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkx.common.exception.APIException;
import com.zkx.system.generator.entity.*;
import com.zkx.system.generator.mapper.AccessMapper;
import com.zkx.common.result.ResultCode;
import com.zkx.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccessServiceImpl extends ServiceImpl<AccessMapper, Access> implements IAccessService{

    @Autowired
    AccessMapper accessMapper;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserServiceImpl userService;

    //根据id删除指定权限
    public String delAccessByAccessId(Integer id,String token){

        Access access = accessMapper.selectAccessByAccessId(id);
        Integer accessId;
        //数据库中查找不到指定Access Id 抛异常
        if(access==null){
            throw new APIException(ResultCode.ACCESS_NOT_EXIST);
        }else{
            accessId = access.getId();
            String function = access.getFunction();
            if(userService.ifTokenExist(token)){
                UandRandA uandRandA = (UandRandA) redisUtil.get(token);
                Integer userId = uandRandA.getId();
                //获取access_role表中有accessId的roleId
                List<Integer> list = accessMapper.selectAccessRoleIdByAccessId(accessId);
                //如果redis中 key为roleId 开头的 value中有需要删除的access 那么就遍历redis中的roleId 开头的key
                if(list.size()!=0){
                    for (int i = 0; i < list.size(); i++) {
                        if(redisUtil.hasKey("roleId "+list.get(i))){
                            //System.out.println("success");
                            List<Object> list1 = redisUtil.lGet("roleId "+list.get(i),0,-1);
                            List<RandA> randAList = new ArrayList<>();
                            for (int j = 0; j < list1.size(); j++) {
                                randAList.add(j,(RandA)list1.get(j));
                            }
                            for (int x = 0; x < randAList.size(); x++) {
                                if(randAList.get(x).getFunction().equals(function)){
                                    redisUtil.del("roleId "+randAList.get(x).getRoleId());
                                    break;
                                }
                            }
                        }
                    }
                }
                //查看redis中 UandA userId 的value是否有需要删除的access
                if(redisUtil.hasKey("UandA "+userId)){

                    List<Object> list1 =  redisUtil.lGet("UandA "+userId,0,-1);
                    List<UandA> list2 = new ArrayList<>();
                    for (int i = 0; i < list1.size(); i++) {
                        list2.add(i,(UandA) list1.get(i));
                    }
                    for (int i = 0; i < list2.size(); i++) {
                        if(list2.get(i).getFunction().equals(function)){
                            redisUtil.del("UandA "+userId);
                            break;
                        }
                    }
                }

                //如果token中有要删除的access 删除token
                for (int i = 0; i < uandRandA.getAccesses().size(); i++) {
                    if(function.equals(uandRandA.getAccesses().get(i))){
                        redisUtil.del(token);
                        redisUtil.del("userId "+userId);
                        StaticClass.setToken("");
                        break;
                    }
                }
                //删除access表中的记录
                accessMapper.deleteById(accessId);
                //查看access_role表中是否有access_id的记录 有的话删除
                if(accessMapper.selectAccessRoleIdByAccessId(accessId).size()!=0){
                    accessMapper.deleteByAccessId(id);
                }
                return "delete success";
            }
            else{
                throw new APIException(ResultCode.USER_NOT_EXIST);
            }
        }
    }
}
